package com.guochenglang.www.service.impl;

import com.guochenglang.www.dao.ArticleDao;
import com.guochenglang.www.dao.LikeMessageDao;
import com.guochenglang.www.dao.LikesDao;
import com.guochenglang.www.pojo.Article;
import com.guochenglang.www.pojo.LikeMessage;
import com.guochenglang.www.pojo.Likes;
import com.guochenglang.www.service.LikesService;
import com.guochenglang.www.service.anotate.Affair;
import com.guochenglang.www.util.GetTable;
import com.guochenglang.www.util.MyDependency;
import com.guochenglang.www.util.lru.MyCapacity;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 点赞业务
 *
 * @author GuoChenglang
 */
public class LikesServiceImpl implements LikesService {
    private static final LikesDao LIKES_DAO = MyDependency.returnObject(LikesDao.class);
    private static final ArticleDao ARTICLE_DAO = MyDependency.returnObject(ArticleDao.class);
    private static final LikeMessageDao LIKE_MESSAGE_DAO = MyDependency.returnObject(LikeMessageDao.class);

    /**
     * 进行点赞
     *
     * @param likeId    点赞人id
     * @param articleId 文章id
     * @return 主键
     */
    @Affair
    @Override
    public int clickLikes(Integer likeId, Integer articleId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        ArrayList<Likes> likes = new ArrayList<>();
        Article condition = new Article();
        //封装条件
        condition.setId(articleId);
        Integer userId = ARTICLE_DAO.articleSelect(condition).get(0).getCreaterId();
        //搜索该用户是否有过点赞该文章的行为
        Likes like = new Likes();
        //封装搜索条件
        like.setArticleId(articleId);
        like.setLikeId(likeId);
        likes = LIKES_DAO.likesSelect(like);
        if (likes.isEmpty()) {
            //未存在点赞行为,进行点赞
            int key = LIKES_DAO.insertLikes(like);
            LIKE_MESSAGE_DAO.insertLikeMessage(new LikeMessage(null, 0, userId, key));
            return key;
        } else {
            //存在点赞行为，取消点赞;
            LIKE_MESSAGE_DAO.deleteLikeMessage(new LikeMessage(null, null, null, likes.get(0).getId()));
            return LIKES_DAO.deleteLikes(like);
        }
    }

    /**
     * 获得点赞个数
     *
     * @param articleId 文章id
     * @return 点赞个数
     */
    @Override
    public int getLikes(Integer articleId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //封装条件
        Likes condition = new Likes();
        condition.setArticleId(articleId);
        return LIKES_DAO.likesSelect(condition).size();

    }

    /**
     * 获得点赞信息
     *
     * @param likeId 点赞id
     * @return 点赞信息
     */
    @Override
    public Likes getLike(Integer likeId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        String key = MyCapacity.getKey(likeId, GetTable.getTable(Likes.class));
        //从缓存获取
        Likes aLike = (Likes) MyCapacity.get(key);
        if (aLike == null) {
            Likes condition = new Likes();
            condition.setId(likeId);
            ArrayList<Likes> likes = LIKES_DAO.likesSelect(condition);
            if (!likes.isEmpty()) {
                aLike = likes.get(0);
                //加入缓存
                MyCapacity.put(key, aLike);
            }
        }
        return aLike;
    }
}
